<template>
  <a-card :bordered="false">
    <div class="table-operator">
        <a-button icon="download" @click="downloadExpertExcel"> 1. 下载模版</a-button>&nbsp;&nbsp;&nbsp;<br/>
      <a-upload
        name="file"
        :remove="handleRemove"
        :beforeUpload="expertDataBeforeUpload"
        :showUploadList="{ showDownloadIcon: true, showRemoveIcon: true }"
        :fileList="fileList"
      >
        <a-button type="primary" icon="plus" > 2 上传国网员报系统专家excel/院士 excel</a-button>
      </a-upload>
      <a-button
        type="primary"
        :disabled="fileList.length === 0"
        :loading="uploading"
        style="margin-top: 16px"
        @click="uploadExpertData(1)"
      >
        {{ uploading ? '上传中...' : '3.1 开始上传（国网员报系统专家）' }}
      </a-button>&nbsp;&nbsp;&nbsp;
      <a-button
          type="danger"
          :disabled="fileList.length === 0"
          :loading="uploading"
          style="margin-top: 16px"
          @click="uploadExpertData(2)"
      >
        {{ uploading ? '上传中...' : '3.2 开始上传（院士专家）' }}
      </a-button>
<!--      <a-divider />-->
<!--      <a-button type="primary" @click="transformDict" icon="swap" :loading="dictLoading" >字典表转换</a-button>-->
<!--      s-->
    <a-divider />
        <a-input
            style="width: calc(100% - 130px)"
            v-model="folderPath"
        /><br/><br/>
      <a-radio-group v-model="importType">
        <a-radio :value="1">
          院士数据（jsonL）
        </a-radio>
        <a-radio :value="2">
          其他专家数据（jsonArray）
        </a-radio>
      </a-radio-group><br/><br/>
      <a-radio-group v-model="expand">
        <a-radio :value="true">
          扩展专家
        </a-radio>
        <a-radio :value="false">
          不扩展专家
        </a-radio>
      </a-radio-group><br/>
        <a-button
            type="danger"
            :loading="folderLoading"
            style="margin-top: 16px"
            @click="importCnkiFruitOfFolder"
        >
           文件太多 直接读取文件夹路径
        </a-button>
        <a-divider />
<!--        <a-button type="primary" @click="dealFruitNewField" icon="swap" :loading="fruitNewFieldLoading" >处理成果领域新分类</a-button>-->
<!--      <a-divider />-->
        <a-upload
            name="file"
            :remove="handleGwFieldKeyWordRemove"
            :beforeUpload="gwFieldKeyWordBeforeUpload"
            :showUploadList="{ showDownloadIcon: true, showRemoveIcon: true }"
            :fileList="fkFileList"
        >
            <a-button type="primary" icon="plus" > 导入国网研究领域关键词</a-button>
        </a-upload>
        <a-button
            type="primary"
            :disabled="fkFileList.length === 0"
            :loading="fkUploading"
            style="margin-top: 16px"
            @click="uploadFkData"
        >
            {{ fkUploading ? '上传中...' : '开始上传' }}
        </a-button>
        <a-divider />
        <a-button type="primary" @click="dealSameExperts" icon="swap" :loading="seLoading" >处理同名专家合并</a-button>
        <a-divider />
        <a-button type="primary" @click="dealFruitWords" icon="swap" :loading="wordLoading" >处理成果关键词及成果领域提取</a-button>
        <a-divider />
        <a-button type="primary" @click="dealTalentFields" icon="swap" :loading="fieldLoading" >处理专家领域关键词</a-button>
        <a-divider />
<!--        <a-button type="primary" @click="dealFruitAndTalentRel" icon="swap" :loading="relLoading" >处理成果信息及专家关联</a-button>-->
<!--        <a-button type="primary" @click="dealFruitKwOfGwKw" icon="swap" :loading="wordLoading" >处理成果关键词和国网关键词的匹配</a-button>-->

    </div>
  </a-card>
</template>

<script>
import { getAction, httpAction, postAction, uploadAction } from '../../api/manage'

  export default {
    name: "TalentTools",
    components: {
    },
    data() {
      return {
        description: '人才协作工具页面',
        //token header
        uploading: false,
        dictLoading: false,
        cnkiLoading: false,
        seLoading: false,
        wordLoading: false,
        relLoading: false,
        fieldLoading: false,
        fkUploading: false,
        fruitNewFieldLoading: false,
        fruitKwOfGwKwLoading: false,
        fileList: [],
        folderPath: "/Users/pikabu/my/work/company/智库/国网/数据/test_ana",
        folderLoading: false,
        fruitFileList: [],
        allFruitFileList: [],
        fkFileList: [],
        headers: {
          authorization: 'authorization-text',
        },
        importType: 1,
        expand: false,
        // 查询条件
        queryParam: {},
        // 表头
        url: {
        },
      }
    },
    computed: {
    },
    methods: {
        downloadExpertExcel(){
            let a = document.createElement('a');
            a.download = "国网专家库专家导入模版.xlsx";
            a.href = "/static/template/expertTemplate.xlsx";
            a.click();
        },
        downloadFruitTxt(){
            let a = document.createElement('a');
            a.download = "国网专家库成果导入模版.txt";
            a.href = "/static/template/fruitTemplate.txt";
            a.click();
        },
      handleRemove(file) {
        const index = this.fileList.indexOf(file);
        const newFileList = this.fileList.slice();
        newFileList.splice(index, 1);
        this.fileList = newFileList;
      },
      expertDataBeforeUpload(file){
          if (
            file.name.substr(file.name.length - 3).toLowerCase() != 'xls' &&
            file.name.substr(file.name.length - 4).toLowerCase() != 'xlsx'
          ) {
            this.$message.warning('只能上传xls、xlsx文件!')
          } else {
            this.fileList = [...this.fileList, file];
          }
        return false;
      },
      uploadExpertData(type){
        this.uploading = true;
        let formData = new FormData()
        this.fileList.forEach(file => {
          formData.append('file', file);
          formData.append('talentType', type);
        });
        // formData.append('file', data.file)
        // formData.append('token', this.tokenHeader) //随便写一个token示例
        let url = type == 1 ? "/ndTalent/talent/common/importExpertDataExcel" : "/ndTalent/talent/common/importYsExpertDataExcel";
        uploadAction(url, formData).then(res => {
          console.log(res);
            if (res.data.success) {
              this.uploading = false;
              this.fileList = [];
              this.$message.success('文件导入成功!')
            } else {
              this.uploading = false;
              this.$message.error('文件导入失败!')
            }
        }).catch(err => {
          console.log(err)
          this.uploading = false;
          this.$message.error('文件导入失败!')
        })
      },
      transformDict(){
        this.dictLoading = true;
        postAction("/ndTalent/talent/common/transformDict", {}).then(res => {
          console.log(res);
          if (res.data.success) {
            this.dictLoading = false;
            this.$message.success('转换成功!')
          } else {
            this.dictLoading = false;
            this.$message.error('转换失败!')
          }
        }).catch(err => {
          console.log(err)
          this.dictLoading = false;
          this.$message.error('转换失败!')
        })
      },
      handleFruitRemove(file) {
        const index = this.fruitFileList.indexOf(file);
        const newFileList = this.fruitFileList.slice();
        newFileList.splice(index, 1);
        this.fruitFileList = newFileList;
      },
      fruitDataBeforeUpload(file){
        // 使用FileReader来读取文件内容
        const reader = new FileReader();
        reader.onload = () => {
          // 文件读取完成后，将文件添加到将要上传的文件列表
          this.fruitFileList.push(file);
          // 模拟异步操作
          setTimeout(() => {
            // 设置为读取完成状态
          }, 1000);
        };
        reader.readAsDataURL(file);

        return false; // 返回false以阻止自动上传
      },
      importCnkiFruit(){
        if(this.fruitFileList.length == 0){
          this.$message.error('请选择文件夹!')
          return false;
        }
        this.cnkiLoading = true;
        let formData = new FormData()
        this.fruitFileList.forEach(file => {
          formData.append('files', file);
        });
        let url = "/ndTalent/talent/common/importCnkiFruit";
        uploadAction(url, formData).then(res => {
          console.log(res);
          if (res.data.success) {
            this.cnkiLoading = false;
            this.fruitFileList = [];
            this.$message.success('导入成功!')
          } else {
            this.cnkiLoading = false;
            this.$message.error('导入失败!')
          }
        }).catch(err => {
          console.log(err)
          this.cnkiLoading = false;
          this.$message.error('导入失败!')
        })
      },
        importCnkiFruitOfFolder(){
        if(!this.folderPath){
          this.$message.error('请填写文件夹路径!')
          return false;
        }
        this.folderLoading = true;
        let folderPath = this.folderPath.replace(/\\/g, '/');
        let url = "/ndTalent/talent/common/importCnkiFruitOfFolder?folderPath=" + encodeURIComponent(folderPath)+"&importType="+this.importType + "&expand=" + this.expand;
        getAction(url).then(res => {
          console.log(res);
          if (res.data.success) {
            this.folderLoading = false;
            this.$message.success('导入成功!')
          } else {
            this.cnkiLoading = false;
            this.$message.error('导入失败!')
          }
        }).catch(err => {
          console.log(err)
          this.folderLoading = false;
          this.$message.error('导入失败!')
        })
      },
      dealSameExperts(){
        this.seLoading = true;
        postAction("/ndTalent/talent/common/dealExpertSimilarity", {}).then(res => {
          console.log(res);
          if (res.data.success) {
            this.seLoading = false;
            this.$message.success('处理成功!')
          } else {
            this.seLoading = false;
            this.$message.error('处理失败!')
          }
        }).catch(err => {
          console.log(err)
          this.seLoading = false;
          this.$message.error('处理失败!')
        })
      },
      dealFruitWords(){
        this.wordLoading = true;
        postAction("/ndTalent/talent/common/dealFruitKeyword", {}).then(res => {
          console.log(res);
          if (res.data.success) {
            this.wordLoading = false;
            this.$message.success('处理成功!')
          } else {
            this.wordLoading = false;
            this.$message.error('处理失败!')
          }
        }).catch(err => {
          console.log(err)
          this.wordLoading = false;
          this.$message.error('处理失败!')
        })
      },
      dealTalentFields(){
        this.fieldLoading = true;
        postAction("/ndTalent/talent/common/dealTalentField", {}).then(res => {
          console.log(res);
          if (res.data.success) {
            this.fieldLoading = false;
            this.$message.success('处理成功!')
          } else {
            this.fieldLoading = false;
            this.$message.error('处理失败!')
          }
        }).catch(err => {
          console.log(err)
          this.fieldLoading = false;
          this.$message.error('处理失败!')
        })
      },
        dealFruitAndTalentRel(){
            this.relLoading = true;
            postAction("/ndTalent/talent/common/dealFruitAndTalentRel", {}).then(res => {
                console.log(res);
                if (res.data.success) {
                    this.relLoading = false;
                    this.$message.success('处理成功!')
                } else {
                    this.relLoading = false;
                    this.$message.error('处理失败!')
                }
            }).catch(err => {
                console.log(err)
                this.relLoading = false;
                this.$message.error('处理失败!')
            })
        },
    dealFruitNewField(){
        this.fruitNewFieldLoading = true;
        postAction("/ndTalent/talent/common/dealFruitNewField", {}).then(res => {
          console.log(res);
          if (res.data.success) {
            this.fruitNewFieldLoading = false;
            this.$message.success('处理成功!')
          } else {
            this.fruitNewFieldLoading = false;
            this.$message.error('处理失败!')
          }
        }).catch(err => {
          console.log(err)
          this.fruitNewFieldLoading = false;
          this.$message.error('处理失败!')
        })
      },
      handleGwFieldKeyWordRemove(file) {
          const index = this.fileList.indexOf(file);
          const newFileList = this.fileList.slice();
          newFileList.splice(index, 1);
          this.fkFileList = newFileList;
      },
      gwFieldKeyWordBeforeUpload(file){
          if (
              file.name.substr(file.name.length - 3).toLowerCase() != 'xls' &&
              file.name.substr(file.name.length - 4).toLowerCase() != 'xlsx'
          ) {
              this.$message.warning('只能上传xls、xlsx文件!')
          } else {
              this.fkFileList = [...this.fkFileList, file];
          }
          return false;
      },
      uploadFkData(){
          this.fkUploading = true;
          let formData = new FormData()
          this.fkFileList.forEach(file => {
              formData.append('file', file);
          });
          let url = "/ndTalent/talent/common/importFkExcel";
          uploadAction(url, formData).then(res => {
              console.log(res);
              if (res.data.success) {
                  this.fkUploading = false;
                  this.fkFileList = [];
                  this.$message.success('文件导入成功!')
              } else {
                  this.uploading = false;
                  this.$message.error('文件导入失败!')
              }
          }).catch(err => {
              console.log(err)
              this.fkUploading = false;
              this.$message.error('文件导入失败!')
          })
      },
        dealFruitKwOfGwKw(){
            this.fruitKwOfGwKwLoading = true;
            postAction("/ndTalent/talent/common/dealFruitKwOfGwKw", {}).then(res => {
                if (res.data.success) {
                    this.fruitKwOfGwKwLoading = false;
                    this.$message.success('处理成功!')
                } else {
                    this.fruitKwOfGwKwLoading = false;
                    this.$message.error('处理失败!')
                }
            }).catch(err => {
                console.log(err)
                this.fruitKwOfGwKwLoading = false;
                this.$message.error('处理失败!')
            })
        },
    },
  }
</script>
<style scoped>
</style>